'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { STRING } = Sequelize;
    
    // 检查status字段是否已存在
    const tableInfo = await queryInterface.describeTable('suppliers');
    if (!tableInfo.status) {
      // 在suppliers表中添加status字段
      await queryInterface.addColumn('suppliers', 'status', {
        type: STRING(20),
        defaultValue: 'pending',
        comment: '状态: pending-待审核, active-合作中, suspended-已暂停, terminated-已终止'
      });
    }
    
    // 检查索引是否已存在
    const indexes = await queryInterface.showIndex('suppliers');
    const statusIndexExists = indexes.some(index => index.name === 'suppliers_idx_status');
    if (!statusIndexExists) {
      // 添加索引
      await queryInterface.addIndex('suppliers', ['status'], {
        name: 'suppliers_idx_status',
        unique: false
      });
    }
  },

  down: async (queryInterface, Sequelize) => {
    // 检查status字段是否存在
    const tableInfo = await queryInterface.describeTable('suppliers');
    if (tableInfo.status) {
      // 删除索引
      try {
        await queryInterface.removeIndex('suppliers', 'suppliers_idx_status');
      } catch (error) {
        // 索引可能不存在，忽略错误
        console.log('Index suppliers_idx_status may not exist, skipping removal');
      }
      
      // 删除status字段
      await queryInterface.removeColumn('suppliers', 'status');
    }
  }
};